package com.mobdev.notepadgdocs;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.util.Log;

import com.mobdev.notepad.shared.AbstractNotepadProvider;
import com.mobdev.notepad.shared.DBConstants;
import com.mobdev.notepad.shared.NotepadStrings;

/**
 * Provides access to a database of notes. Each note has a title, the note
 * itself, a creation date and a modified data.
 */
public class NotepadProvider extends AbstractNotepadProvider {

	
	public static final String LOCALYTICS_APP_KEY = "1ba8fe82e263c2371170d99-a7dea69c-a3ff-11de-7a27-00b7ea894fbb";
	
	public static final String AUTHORITY = "com.mobdev.notepadgdocs.provider.NotePad";
	public static final Uri NOTEPAD_URI = Uri.parse("content://" + AUTHORITY + "/notes");

    private static final int DATABASE_VERSION = 2;

	
    /**
     * The MIME type of {@link #CONTENT_URI} providing a directory of notes.
     */
    public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.mobdev.note";


    /**
     * The MIME type of a {@link #CONTENT_URI} sub-directory of a single note.
     */
    public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.mobdev.note";

    
    /**
     * This class helps open, create, and upgrade the database file.
     */
    private static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) 
        {
        	//creates notes table
            db.execSQL("CREATE TABLE " + NOTES_TABLE_NAME + " ("
                    + DBConstants._ID + " INTEGER PRIMARY KEY,"
                    + DBConstants.REMOTE_ID + " TEXT,"
                    + DBConstants.TITLE + " TEXT,"
                    + DBConstants.CONTENTS + " TEXT,"
                    + DBConstants.TAGS + " TEXT,"
                    + DBConstants.CREATED_ON + " INTEGER,"
                    + DBConstants.MODIFIED_ON + " INTEGER,"
                    + DBConstants.SYNCED_ON + " INTEGER,"                    
                    + DBConstants.NEW_NOTE_CREATED_ON_PHONE + " TINYINT,"
                    + DBConstants.DELETED_FROM_PHONE + " TINYINT,"
                    + DBConstants.DONT_SYNC_THIS_NOTE + " INTEGER"                    
                    + ");");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        	Log.w(NotepadStrings.LOG_TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ".");
	        
        	if(oldVersion < 2)
        	{
        		//update modified time so that the deletes get synced
        		Log.w(NotepadStrings.LOG_TAG, "Adding new DONT_SYNC_THIS_NOTE column with INTEGER type so it can store time");
        		//int now = (int) (System.currentTimeMillis()/1000);
        		db.execSQL("ALTER TABLE " + NOTES_TABLE_NAME + " ADD " + DBConstants.DONT_SYNC_THIS_NOTE + " INTEGER");
        		
        		ContentValues values = new ContentValues();
            	values.put(DBConstants.DONT_SYNC_THIS_NOTE, 0);        		
        		db.update(NOTES_TABLE_NAME, values, null, null);
        		
        	}
        	
        	//else 
        	//{
        	//	Log.w(TAG, "Destroying old data.");
	        //    db.execSQL("DROP TABLE IF EXISTS notes");
	        //    onCreate(db);
        	//}
            
        }
    }

	@Override
	protected String getAuthority() {
		return AUTHORITY;
	}


	@Override
	protected String getContentType() {
		return CONTENT_TYPE;
	}

	@Override
	protected String getContentItemType() {
		return CONTENT_ITEM_TYPE;
	}

	@Override
	protected SQLiteOpenHelper getSQLiteOpenHelper() {
		return new DatabaseHelper(getContext());
	}

}
